const router = require("koa-router")();
const Menu = require("../models/menuSchema");
const utils = require("../utils/responseUtil");
const log = require("../utils/log4jsHelper");
router.prefix("/menus");

// router.get("/", function (ctx, next) {
//   ctx.body = "this is a users response!";
// });

// router.get("/login", function (ctx, next) {
//   ctx.body = "this is a users/bar response";
// });

router.get("/list", async (ctx) => {
  try {
    const list = await Menu.find().populate('children');
    log.debug(`查询数据库,结果为,list=${list}`);
    if (list) {
      ctx.body = utils.success(list, "查询所有菜单列表成功", 200);
    } else {
      ctx.body = utils.fail("查询所有菜单列表失败");
    }
  } catch (error) {
    log.debug(`查询所有菜单列表出错,error=${error}`);
    ctx.body = utils.fail(error.message);
  }
});

module.exports = router;
